"use strict";
var _menuID, _nodeID, _userID, _functionID;
var $container, $selectYear, $table, _idField, _stage, _latlon;

function init(stage) {
	if(!stage) {
		return;
	}
	_stage = stage;

	_userID = vw_static.getCookie(window.location.host);
	_functionID = vw_static.getQueryString(window, "fid");
	$container = $('#structure');
	$selectYear = $('#selectYear');

	getCheckStat("数据状态");
	initYear();
	initTable();
}

function initYear() {
	$selectYear.select2({
		placeholder: "请选择...",
		language: "zh-CN"
	});

	getyear();

	for(var i = 0; i < _year.years.length; i++) {
		$selectYear.append('<option value="' + _year.years[i] + '">' + _year.years[i] + '</option>');
	};
	$selectYear.on('select2:select', function(e) {
		if(_nodeID) {
			initTable();
		}
	});
	$selectYear.val(_year.currentYear).trigger('select2:select');
}

function initTable() {
	$container.vw_structure('destroy').vw_structure({
		searchtree: {
			menu: [{
				id: 1,
				text: "按报表分类"
			}, {
				id: 2,
				text: "按调查对象分类"
			}],
			defaultMenuID: _menuID,
			selectNodeID: _nodeID ? _nodeID.id : "",
			onTreeSelected: function(node, menu) {
				_nodeID = {
					id: node.id,
					text: node.text,
					menu: menu
				};
			},
			onMenuChanged: function(menu) {
				_menuID = menu;
			}
		},
		edittable: {
			showExport: false,
			editable: false,
			operate: operate,
			ExtraTools: [
				'<button type="button" id="fn_exportData" class="btn btn-primary"><i class="fa fa-download"></i> 导出</button>',
				'<a download="" id="exportUrl" href=""></a>',
				'<button type="button" id="fn_delete" class="btn btn-danger"><i class="fa fa-remove"></i> 删除</button>'
			].join(''),
			columnFormatter: _columnFormatter,
			onInitialized: function() {
				$('#fn_exportData').click(fn_exportData);
				$('#fn_delete').click(fn_delete);
			},
			onAllEvents: function() {
				$('#fn_exportData').prop('disabled', !$container.vw_structure('getEdittable').vw_edittable('getSelections').length);
				$('#fn_delete').prop('disabled', !$container.vw_structure('getEdittable').vw_edittable('getSelections').length);
			}
		},
		showMap: false,
		interfaceAddress: VW_INTERFACE.WEBSERVICE.DataManage,
		extrapara: {
			userID: _userID,
			functionID: _functionID,
			checkstage: _stage,
			year: $selectYear.val()
		}
	});

	$table = $container.vw_structure('getEdittable');
	_idField = $table.vw_edittable('getOptions').idField;
}

function fn_exportData() {
	var ids = $.map($table.vw_edittable('getSelections'), function(row) {
		return row[_idField];
	});

	vw_common.customAjax({
		url: VW_INTERFACE.WEBSERVICE.DataManage('DownloadReport'),
		type: "GET",
		dataType: "json",
		data: {
			id: ids.join(',')
		}
	}, function(e) {
		var pathArray = e.path;
		if(pathArray.length) {
			for(var i = 0; i < pathArray.length; i++) {
				var a = document.getElementById("exportUrl");
				var pathUrl = pathArray[i];
				a.href = pathUrl;
				a.download = pathUrl;
				a.click();
				sleep(2000);
			}
		} else {
			$.Notify({
				caption: "错误",
				content: "无数据",
				type: "alert"
			});
		}
	});
}

function sleep(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while(true) {
		now = new Date();
		if(now.getTime() > exitTime)
			return;
	}
}

function fn_delete() {
	bDialog.alert('确认删除吗？删除后将无法恢复！', function() {
		var ids = $.map($table.vw_edittable('getSelections'), function(row) {
			return row[_idField];
		});

		vw_common.customAjax({
			url: VW_INTERFACE.WEBSERVICE.DataManage('DataDelete'),
			type: "GET",
			dataType: "json",
			data: {
				userID: _userID,
				id: ids.join(',')
			}
		}, function(e) {
			init(_stage);
			$.Notify({
				caption: "成功",
				content: "数据已删除",
				type: "success"
			});
		});
	}, {
		messageType: 'confirm',
		cancel: function() {}
	});
}